package com.example.pulseanalyse.dealRealPointBLE;


import android.util.Log;

import com.example.hyg_veintype.utils.chartUtil.RealTimeChartUtils;
import com.example.hyg_veintype.utils.chartUtil.RealTimeChartUtils2;

import java.math.BigDecimal;
import java.util.LinkedList;

/**
 * 蓝牙接受数据处理（基本与前端一致）
 */
public class PointUtils2 {
    public LinkedList<Integer> allData = new LinkedList<>();
    public LinkedList<Integer> fuData = new LinkedList<>();
    public LinkedList<Integer> zhongData = new LinkedList<>();
    public LinkedList<Integer> chenData = new LinkedList<>();
    public static final String POSITION_CUN = "cun";
    public static final String POSITION_GUAN = "guan";
    public static final String POSITION_CHI = "chi";
    public static final int PRESSURE_FU = 1;
    public static final int PRESSURE_ZHONG = 2;
    public static final int PRESSURE_CHEN = 3;
    //存放实时动压的队列(初始数据)
    LinkedList<Integer> queueData = new LinkedList<>();
    public LinkedList<Integer> drawPointY = new LinkedList<>();
    LinkedList<Integer> baseData = new LinkedList<>();
    // 王工
    int Flt;
    //队列数据和
    int nHe;
    //队列头旧值
    int nOld;
    //基线的高度  
    int nBLHeigt;
    //0x0bb8
    int nSclNum;

    public PointUtils2() {
        //存放实时动压的队列(初始数据)
        queueData.clear();
        drawPointY.clear();
        baseData.clear();
        Flt = 120;
        //队列中数据和
        nHe = 270 * 100;
        //新的值
        nOld = 270;
        //基线的高度  
        nBLHeigt = 120;
        //0x0bb8  可以控制波幅大小
        nSclNum = 3500;
        //初始化一条长度1478Y值50的直线
        for (int i = 0; i < RealTimeChartUtils2.POINT_MAX; i++) {
            queueData.add(80);
            drawPointY.add(80);
            baseData.add(80);
        }
    }

    /**
     * 是否是设备采集用到的压力数据
     *
     * @param bytes （头部第四位判断）
     * @return
     */
    public boolean isPressureData(byte[] bytes) {
        if (bytes != null && bytes.length > 3 && bytes[3] == 0x8) {
            return true;
        }
        return false;
    }

    /**
     * 获得实时静压
     */
    public int getStaticPressure(byte[] bytes) {
        int pulseStaticPressureValue = (bytes[4] << 8) | bytes[5];
        return pulseStaticPressureValue;
    }

//    public synchronized int getDynamicPressure(int pressure){
//        int dealData=pressure-1400;
//        allData.add(dealData);
//        return dealData;
//    }

    /**
     * 处理蓝牙接受的byte数据转换成点的Y坐标值（动压）
     */
    public synchronized int getDynamicPressure(byte[] bytes, String postion,int pressure) {

        BigDecimal bigDecimal1=new BigDecimal("50");
        BigDecimal bigDecimal2=new BigDecimal("30");
        BigDecimal modulus=bigDecimal2.divide(bigDecimal1,2,BigDecimal.ROUND_HALF_UP);
        //动压
        int receveData = 0;
        if (POSITION_CUN.equals(postion)) {
            receveData = ((int) (((bytes[6] & 0xFF) << 8) | (bytes[7] & 0xFF))-1400);
            receveData=new BigDecimal(receveData+"").multiply(modulus).intValue();
            Log.d("receveCun===", receveData + "");
        } else if (POSITION_GUAN.equals(postion)) {
            receveData = ((int) (((bytes[8] & 0xFF) << 8) | (bytes[9] & 0xFF))-1400);
            receveData=new BigDecimal(receveData+"").multiply(modulus).intValue();
            Log.d("receveGuan===", receveData + "");
        } else if (POSITION_CHI.equals(postion)) {
            receveData = ((int) (((bytes[10] & 0xFF) << 8) | (bytes[11] & 0xFF))-1400);
            receveData=new BigDecimal(receveData+"").multiply(modulus).intValue();
            Log.d("receveChi===", receveData + "");
        }
        if (receveData <= 0) {
            Log.d("分===="+pressure,receveData+"");
            return 0;
        }
        if(pressure==PRESSURE_FU){
            fuData.add(receveData);
        }
        if(pressure==PRESSURE_ZHONG){
            zhongData.add(receveData);
        }
        if(pressure==PRESSURE_CHEN){
            chenData.add(receveData);
        }
        queueData.poll();
        queueData.add(receveData);
        drawPointY = queueData;
        baseData.poll();
        baseData.add(receveData);
        //存放所有数据点
        allData.add(receveData);

        return receveData;
    }

}
